package com.tanhua.mqlistener;

import com.tanhua.api.mongo.PublishApi;
import com.tanhua.dao.LogMapper;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.template.HuaWeiUGCTemplate;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@RocketMQMessageListener(
        topic = "tanhua-quanzi",
        consumerGroup="tanhua-quanzi-c"
)
@Component
public class PublishListener implements RocketMQListener<String> {

    @Autowired
    private LogMapper logMapper;

    @Reference
    private PublishApi publishApi;


    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override  //队列中有数据 该方法就执行
    public void onMessage(String publishId) {

        //根据id查询动态
        Publish publish = publishApi.findById(publishId);

        if(publish.getState()==0){
            //实现文字审核
            boolean flag1 = huaWeiUGCTemplate.textContentCheck(publish.getTextContent());
            //实现图片审核
            List<String> list = publish.getMedias();
            boolean flag2 = huaWeiUGCTemplate.imageContentCheck(list.toArray(new String[]{}));

            Integer state=2; //审核不通过
            if(flag1 && flag2){
                state=1; //审核通过
            }

            publishApi.updateState(publishId,state);
        }


        System.out.println("已经审核完毕，是否成功查看数据库...");

    }
}
